package com.hog.web.response;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hog.enums.StateEnum;

import java.util.List;

/**
 * HogResponse
 *
 * @author 糖先森
 * @date 2018年03月30日
 * @since JDK 1.8
 */
public class HogResponse<T> {
    private int code;
    private String msg;
    private T data;

    public HogResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 返回给前台DataGrid所需要的JSON格式数据
     *
     * @param list
     * @return
     */
    public static String retDataGridJson(List<?> list) {
        PageInfo<?> page = new PageInfo<>(list);
        JSONObject object = new JSONObject();
        object.put("total", page.getTotal());
        object.put("rows", list);
        return object.toJSONString();
    }

    public static <T> HogResponse ret(StateEnum state, T data) {
        return new HogResponse<>(state.getCode(), state.getMsg(), data);
    }

    public static <T> HogResponse ret(int code, String msg, T data) {
        return new HogResponse<>(code, msg, data);
    }

    public static HogResponse ret(StateEnum state) {
        return ret(state, null);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}